/**
 * Copyright (c) 2015-2016
 */
package com.tyf.common.domain;

import org.apache.commons.lang3.StringUtils;

import javax.persistence.Column;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;


/**
 * @Description: 抽象树的实现<br>
 * @author xd<br>
 * @date Aug 13, 2016 2:18:15 PM<br>
 *
 */
@MappedSuperclass
public abstract class AbstractTreeableEntity<ID extends Serializable> extends BaseEntity<ID> {

    /**
     * @Fields serialVersionUID :
     */
    private static final long serialVersionUID = 1L;

    /**
     * 标题
     */
    private String name;

    /**
     * 父ID
     */
    @Column(name = "parent_id")
    private ID parentId;

    /**
     * 父路径
     */
    @Column(name = "parent_ids")
    private String parentIds;

    /**
     * 权重
     */
    private Integer weight;

    /**
     * 图标
     */
    private String icon;

    /*
     * @Title: setName<br>
     * 
     * @Description:<br>
     * 
     * @param name <br>
     * 
     * @see Treeable#setName(java.lang.String)
     */
       
    public void setName(String name) {
        this.name = name;
    }

    /*
     * @Title: getName<br>
     * 
     * @Description:<br>
     * 
     * @return <br>
     * 
     * @see Treeable#getName()
     */

    public String getName() {

        return this.name;
    }

    /*
     * @Title: getIcon<br>
     * 
     * @Description:<br>
     * 
     * @return <br>
     * 
     * @see Treeable#getIcon()
     */

    public String getIcon() {

        if (!StringUtils.isEmpty(this.icon)) {
            return this.icon;
        }
        if (isRoot()) {
            return getRootDefaultIcon();
        }
        if (isLeaf()) {
            return getLeafDefaultIcon();
        }
        return getBranchDefaultIcon();

    }

    /*
     * @Title: setIcon<br>
     * 
     * @Description:<br>
     * 
     * @param icon <br>
     * 
     * @see Treeable#setIcon(java.lang.String)
     */

    public void setIcon(String icon) {

        this.icon = icon;
    }

    /*
     * @Title: getParentId<br>
     * 
     * @Description:<br>
     * 
     * @return <br>
     * 
     * @see Treeable#getParentId()
     */

    public ID getParentId() {

        return this.parentId;
    }

    /*
     * @Title: setParentId<br>
     * 
     * @Description:<br>
     * 
     * @param parentId <br>
     * 
     * @see Treeable#setParentId(java.io.Serializable)
     */

    public void setParentId(ID parentId) {
        this.parentId = parentId;

    }

    /*
     * @Title: getParentIds<br>
     * 
     * @Description:<br>
     * 
     * @return <br>
     * 
     * @see Treeable#getParentIds()
     */

    public String getParentIds() {
        return this.parentIds;
    }

    /*
     * @Title: setParentIds<br>
     * 
     * @Description:<br>
     * 
     * @param parentIds <br>
     * 
     * @see Treeable#setParentIds(java.lang.String)
     */

    public void setParentIds(String parentIds) {
        this.parentIds = parentIds;
    }

    /*
     * @Title: getSeparator<br>
     * 
     * @Description: 路径分隔符 <br>
     * 
     * @return <br>
     * 
     * @see Treeable#getSeparator()
     */

    public String getSeparator() {
        return "/";
    }

    /*
     * @Title: makeSelfAsNewParentIds<br>
     * 
     * @Description:<br>
     * 
     * @return <br>
     * 
     * @see Treeable#makeSelfAsNewParentIds()
     */

    public String makeSelfAsNewParentIds() {
        return getParentIds() + getId() + getSeparator();
    }

    public String getTreetableIds() {
        String selfId = makeSelfAsNewParentIds().replace("/", "-");
        return selfId.substring(0, selfId.length() - 1);
    }

    public String getTreetableParentIds() {
        String parentIds = getParentIds().replace("/", "-");
        return parentIds.substring(0, parentIds.length() - 1);
    }

    /*
     * @Title: getWeight<br>
     * 
     * @Description:<br>
     * 
     * @return <br>
     * 
     * @see Treeable#getWeight()
     */

    public Integer getWeight() {

        return this.weight;
    }

    /*
     * @Title: setWeight<br>
     * 
     * @Description:<br>
     * 
     * @param weight <br>
     * 
     * @see Treeable#setWeight(java.lang.Integer)
     */

    public void setWeight(Integer weight) {

        this.weight = weight;
    }

    /*
     * @Title: isRoot<br>
     * 
     * @Description:<br>
     * 
     * @return <br>
     * 
     * @see Treeable#isRoot()
     */

    public boolean isRoot() {
        return getParentId() != null && getParentId().equals(0);
    }

    /*
     * @Title: isLeaf<br>
     * 
     * @Description:<br>
     * 
     * @return <br>
     * 
     * @see Treeable#isLeaf()
     */

    public boolean isLeaf() {
        if (isRoot()) {
            return false;
        }
        return !isHasChildren();

    }

    /*
     * @Title: isHasChildren<br>
     * 
     * @Description:<br>
     * 
     * @return <br>
     * 
     * @see Treeable#isHasChildren()
     */

    public boolean isHasChildren() {
        return false;
    }

    public void setHasChildren(boolean hasChildren) {

    }

    /**
     * 根节点默认图标 如果没有默认 空即可
     *
     * @return
     */
    public String getRootDefaultIcon() {
        return "ztree_setting";
    }

    /**
     * 树枝节点默认图标 如果没有默认 空即可
     *
     * @return
     */
    public String getBranchDefaultIcon() {
        return "ztree_folder";
    }

    /**
     * 树叶节点默认图标 如果没有默认 空即可
     *
     * @return
     */
    public String getLeafDefaultIcon() {
        return "ztree_file";
    }
}
